<template>
    <div>
        <table>
            <tr>
                <td>名称</td>
                <td>
                    <input type="text" v-model="state.prname">
                </td>
            </tr>
            <tr>
                <td>电话</td>
                <td><input type="text" v-model="state.prphone"></td>
            </tr>
            <tr>
                <td>余额</td>
                <td><input type="text" v-model="state.prprice"></td>
            </tr>
            <tr>
                <td>积分</td>
                <td>
                    <input type="text" v-model="state.prjifen">
                </td>
            </tr>
            <tr>
                <td>性别</td>
                <td><input type="text" v-model="state.prsex"></td>
            </tr>
            <tr>
                <td>时间</td>
                <td><input type="date" v-model="state.prdate"></td>
            </tr>
            <tr>
                <td>图片</td>
                <td>
                    <input type="file" @change="shang">
                    <img :src="state.primage" alt="" width="50px" height="50px" v-if="src!=''">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" value="添加" @click="add" />
                </td>
                <td></td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import { reactive,onMounted,toRefs } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios from 'axios'
const route = useRoute()
const router = useRouter()
const data:any = reactive({
    state:{
       prname:'',
       prphone:'',
       prprice:'',
       prjifen:'',
       prsex:'',
       prdate:'',
       primage:'',
    },
    src:''
})

const {state,src} = toRefs(data)

const shang = (e:any) => {
    let file = e.target.files[0]
    let from = new FormData()
    from.append('file', file)
    axios({
        url:'https://localhost:7284/api/image/shangchuan',
        method:'post',
        data:from,
    }).then((res) => {
        src.value= res.data.data
        state.value.primage = res.data
    })
}




const add = () => {
    if(state.value.prname=="")
    {
        alert('名称不能为空')
        return
    }
    if(state.value.prphone=="")
    {
        alert('电话不能为空')
        return
    }
    if(state.value.prprice=="")
    {
        alert('单价不能为空')
        return
    }
    if(state.value.prjifen=="")
    {
        alert('积分不能为空')
        return
    }
    if(state.value.prsex=="")
    {
        alert('性别不能为空')
        return
    }
    if(state.value.prdate=="")
    {
        alert('时间不能为空')
        return
    }

    
    axios({
        url:'https://localhost:7284/api/Proper/AddProper',
        method:'post',
        data:state.value

    }).then((res) => {
        console.log(res);
        if(res.data.data>0)
        {
            alert('添加成功')
            router.push('/ProperShowView')
        }
        else
        {
            alert('添加失败')
        }
    })
}



</script>

<style scoped></style>